class AddCounterCache < ActiveRecord::Migration
  def self.up
    add_column :users, :submitted_issues_count, :integer, :default => 0
    add_column :users, :subscribed_issues_count, :integer, :default => 0
    add_column :users, :adopted_issues_count, :integer, :default => 0
    add_column :users, :projects_count, :integer, :default => 0
    add_column :issues, :comments_count, :integer, :default => 0
    add_column :issues, :attachments_count, :integer, :default => 0
    add_column :issues, :subscribers_count, :integer, :default => 0
    add_column :issues, :owners_count, :integer, :default => 0
    add_column :projects, :issues_count, :integer, :default => 0
    add_column :projects, :members_count, :integer, :default => 0
    add_column :comments, :attachments_count, :integer, :default => 0
    add_column :comments, :issue_changes_count, :integer, :default => 0

    User.reset_column_information
    User.find(:all).each do |u|
      User.update_counters u.id, :submitted_issues_count => Issue.count( :all, :conditions => ['submitter_id = ?', u]),
                                 :subscribed_issues_count => Subscription.count( :all, :conditions => ['user_id = ?', u]),
                                 :adopted_issues_count => Adoption.count( :all, :conditions => ['user_id = ?', u]),
                                 :projects_count => Membership.count( :all, :conditions => ['user_id = ?', u])
    end

    Issue.reset_column_information
    Issue.find(:all).each do |i|
      Issue.update_counters i.id, :comments_count => Comment.count(:all, :conditions => ['commentable_type = ? AND commentable_id = ?', i.class.to_s, i]),
                                  :attachments_count => AttachmentLink.count(:all, :conditions => ['attachable_type = ? AND attachable_id = ?', i.class.to_s, i]),
                                  :subscribers_count => Subscription.count(:all, :conditions => ['issue_id = ?', i]),
                                  :owners_count => Adoption.count(:all, :conditions => ['issue_id = ?', i])
    end

    Project.reset_column_information
    Project.find(:all).each do |p|
      Project.update_counters p.id, :issues_count => Issue.count(:all, :conditions => ['project_id = ?', p]),
                                    :members_count => Membership.count(:all, :conditions => ['project_id = ?', p])
    end

    Comment.reset_column_information
    Comment.find(:all).each do |c|
      Comment.update_counters c.id, :attachments_count => AttachmentLink.count(:all, :conditions => ['attachable_type = ? AND attachable_id = ?', c.class.to_s, c]),
                                    :issue_changes_count => IssueChange.count(:all, :conditions => ['comment_id = ?', c])
    end
  end

  def self.down
    remove_column :users, :submitted_issues_count
    remove_column :users, :subscribed_issues_count
    remove_column :users, :adopted_issues_count
    remove_column :users, :projects_count
    remove_column :issues, :comments_count
    remove_column :issues, :attachments_count
    remove_column :issues, :subscribers_count
    remove_column :issues, :owners_count
    remove_column :projects, :issues_count
    remove_column :projects, :members_count
    remove_column :comments, :attachments_count
    remove_column :comments, :issue_changes_count
  end
end
